class Edge{
    constructor(g,from,to,$state='new'){
        Edge.idGenerator++;
        this.graph = g;
        this.from = from;
        this.to = to;
        this.$state = $state;
        this.$removed = false;
        this.id = Edge.idGenerator;
    }

    source(){
        return this.graph.getNode(this.from);
    }

    target(){
        return this.graph.getNode(this.to);
    }

    linkType(){
        let edgeType = "linkTo";
        switch (this.graph.getNode(this.to).$type){
            case 'Component':
            case 'ComponentGroup':
                edgeType = (this.graph.getNode(this.from).$type=='ComponentGroup') ? 'contains' : 'of';
                break;
            case 'Feature':
            case 'FeatureGroup':
                edgeType = (this.graph.getNode(this.from).$type=='FeatureGroup') ? 'contains' : 'hasFeatures';
                break;
            case 'Reason':
            case 'ReasonGroup':
                edgeType = (this.graph.getNode(this.from).$type=='ReasonGroup') ? 'contains' : 'hasReasons';
                break;
        }
        return edgeType;
    }

    persist(){
        if(this.$state=='new'){
            if(this.$removed)
                return;
            let session = neo4jService.getSession();
            let resultPromise = session.run(`start f=node(${this.source().db_id}),t=node(${this.target().db_id}) create (f)-[r:${this.linkType()}]->(t) return r `);
            resultPromise.then(result => {
                session.close();
            });
        }else //if(this.$state=='dirty')
        {
            if(this.$removed){
                let session = neo4jService.getSession();
                let resultPromise = session.run(`start f=node(${this.source().db_id}),t=node(${this.target().db_id}) match (f)-[r]->(t) delete r `);
                resultPromise.then(result => {
                    session.close();
                });
            }else{
                //do nothing
            }
        }
    }
}
Edge.idGenerator=0;
